﻿#include "pch.h"

namespace fs = std::filesystem;

// 采用测试前置条件：GC设置采用方法时长2分钟。
class AnalysisTest1 : public ::testing::Test
{
protected:
    void SetUp() override
    {
        std::cout << "SetUp" << std::endl;
        // 创建测试用的OrgFile句柄
        testHandle = createOrgFile("sampling_test");
        pChannelSampling = getChannelSamplingPtr();
        ASSERT_GT(testHandle, 0);

        // 预设谱图信息
        TOrgHead orgHead;
        EXPECT_TRUE(getOrgHead(testHandle, &orgHead));
        orgHead.Sample[0].Minutes = orgHead.Sample[1].Minutes = 200; // 采样时长2分钟，120秒
        orgHead.Detector = 0x0001ffff;
        orgHead.Frequency = 20;
        EXPECT_TRUE(setOrgHead(testHandle, &orgHead));

        // 初始化采样配置
        initBridgeAnalysis("config.ini");

        tagMotherBoardInfo motherBoardInfo[2] = {{0, "GC1949", "1.0"}, {1, "GC1949", "1.0"}};
        tagTIPPortInfo redisAddr = {0, "127.0.0.1", 6379};
        EXPECT_EQ(ChemDevLayerInit((LPBYTE)&motherBoardInfo[0], (LPBYTE)&redisAddr), 0);

        tagTIPPortInfo IPPortInfo = {1, "192.168.114.155", 9100};
        EXPECT_EQ(ChemDevLayerConnect(0, (LPBYTE)&IPPortInfo), 0);
        std::this_thread::sleep_for(3s);
        EXPECT_EQ(ChemDevLayerInitGC(0), 0);

        std::this_thread::sleep_for(3s);
    }

    void TearDown() override
    {
        std::cout << "TearDown" << std::endl;
        if (testHandle > 0)
        {
            closeOrgFile(testHandle);
        }
        EXPECT_EQ(ChemDevLayerDisConnect(0), 0);
        std::this_thread::sleep_for(5s);
        if (fs::exists("saved.method"))
        {
            fs::remove("saved.method");
        }
        if (fs::exists("saved.org"))
        {
            fs::remove("saved.org");
        }
    }

    int64_t testHandle = -1;
    TChannelSampling *pChannelSampling = nullptr;
};
// 测试用例1：谱图方法综合测试一，存储方法和谱图。
TEST_F(AnalysisTest1, OrgMethodSaveFileTest)
{
    // 创建方法
    int64_t methodHandle = createMethodFile("test_method");

    // 设置方法头信息
    std::unique_ptr<TMethodHead> methodHead = std::make_unique<TMethodHead>();
    ASSERT_TRUE(getMethodHead(methodHandle, METHOD_HANDLE, methodHead.get()));
    methodHead.get()->EPara.based = ON_AREA;
    methodHead.get()->EPara.quantBy = BY_EXT_STD;
    methodHead.get()->IPara.pkDrift = 0;
    methodHead.get()->IPara.testPkWidth = methodHead.get()->IPara.pkWidth = (float)0.03;
    methodHead.get()->IPara.testPkNoise = methodHead.get()->IPara.pkNoise = 10;
    methodHead.get()->IPara.testPkDrift = methodHead.get()->IPara.pkDrift = 0;
    methodHead.get()->IPara.initTime = 0;
    methodHead.get()->IPara.stopTime = 2;
    methodHead.get()->IPara.minArea = 0;
    methodHead.get()->IPara.minHeight = 0;
    methodHead.get()->IPara.wdMode = WD_AUTO;
    methodHead.get()->IPara.tdouble = 0;
    methodHead.get()->IPara.tailMode = 0;
    methodHead.get()->IPara.autoNegative = 1;
    methodHead.get()->IPara.shouldSens = 0;
    methodHead.get()->IPara.tprgUsed = 1;
    methodHead.get()->IPara.width = 0;
    methodHead.get()->IPara.slope = 0;
    methodHead.get()->IPara.drift = 0;
    STRING_COPY("非甲烷总烃", methodHead.get()->Name);
    EXPECT_TRUE(setMethodHead(methodHandle, METHOD_HANDLE, methodHead.get()));

    // 设置方法组分信息
    std::unique_ptr<TCompList[]> compList = std::make_unique<TCompList[]>(4);

    // 通道1组分
    STRING_COPY("总烃", compList.get()[0].Name);
    compList.get()[0].order = 0;
    compList.get()[0].f0 = 0;
    compList.get()[0].f1 = (float)49.47902;
    compList.get()[0].f2 = 0;
    compList.get()[0].f3 = 0;
    compList.get()[0].Time = (float)0.248;
    compList.get()[0].band = (float)0.215;
    compList.get()[0].channelNo = 0;
    STRING_COPY("甲烷", compList.get()[1].Name);
    compList.get()[1].order = 1;
    compList.get()[1].f0 = 0;
    compList.get()[1].f1 = (float)9.8889;
    compList.get()[1].f2 = 0;
    compList.get()[1].f3 = 0;
    compList.get()[1].Time = (float)0.748;
    compList.get()[1].band = (float)0.213;
    compList.get()[1].channelNo = 0;

    // 通道2组分
    STRING_COPY("总烃", compList.get()[2].Name);
    compList.get()[2].order = 0;
    compList.get()[2].f0 = 0;
    compList.get()[2].f1 = (float)49.47902;
    compList.get()[2].f2 = 0;
    compList.get()[2].f3 = 0;
    compList.get()[2].Time = (float)0.248;
    compList.get()[2].band = (float)0.215;
    compList.get()[2].channelNo = 1;
    STRING_COPY("甲烷", compList.get()[3].Name);
    compList.get()[3].order = 1;
    compList.get()[3].f0 = 0;
    compList.get()[3].f1 = (float)9.8889;
    compList.get()[3].f2 = 0;
    compList.get()[3].f3 = 0;
    compList.get()[3].Time = (float)0.748;
    compList.get()[3].band = (float)0.213;
    compList.get()[3].channelNo = 1;

    EXPECT_TRUE(setMethodCompList(methodHandle, METHOD_HANDLE, compList.get(), 4));

    // 保存方法
    EXPECT_TRUE(saveMethodFile(methodHandle, "saved.method"));
    EXPECT_TRUE(fs::exists("saved.method"));
    // 保存方法到特定文件，以便后续测试。
    EXPECT_TRUE(saveMethodFile(methodHandle, "D:\\saved.method"));
    EXPECT_TRUE(fs::exists("D:\\saved.method"));

    // 设置谱图仪器条件
    std::unique_ptr<TInstCond[]> instCond = std::make_unique<TInstCond[]>(4);
    STRING_COPY("测试条件1", instCond.get()[0].Name);
    STRING_COPY("测试条件2", instCond.get()[1].Name);
    STRING_COPY("仪器1", instCond.get()[0].InstHead.Name);
    STRING_COPY("仪器2", instCond.get()[1].InstHead.Name);
    instCond.get()[0].ColumnCond.Type = 3;
    instCond.get()[1].ColumnCond.Type = 4;
    for (int32_t chn = 0; chn < 2; chn++)
    {
        EXPECT_TRUE(setOrgInstCond(testHandle, chn, &instCond.get()[chn]));
    }

    // 交换组分名称位置
    STRING_COPY("甲烷", compList.get()[0].Name);
    STRING_COPY("总烃", compList.get()[1].Name);
    STRING_COPY("甲烷", compList.get()[2].Name);
    STRING_COPY("总烃", compList.get()[3].Name);
    EXPECT_TRUE(setMethodCompList(testHandle, ORG_HANDLE, compList.get(), 4));
    // 设置谱图方法
    EXPECT_TRUE(setMethodHead(testHandle, ORG_HANDLE, methodHead.get()));

    // 开始采样
    EXPECT_EQ(ChemDevLayerInstGCStart(0, false, 0, 0), 0);
    EXPECT_TRUE(startSample(testHandle));

    // 等待2分钟
    std::this_thread::sleep_for(120s);

    // 结束采样
    EXPECT_EQ(ChemDevLayerInstGCStop(0), 0);
    stopSample(testHandle);

    // 保存谱图
    EXPECT_TRUE(saveOrgFile(testHandle, "saved.org"));
    EXPECT_TRUE(fs::exists("saved.org"));
    // 保存方法到特定文件，以便后续测试。
    EXPECT_TRUE(saveOrgFile(testHandle, "D:\\saved.org"));
    EXPECT_TRUE(fs::exists("D:\\saved.org"));

    closeMethodFile(methodHandle);
    std::this_thread::sleep_for(3s);
}
#ifdef TEST_SAMPLING
int32_t main(int32_t argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
#endif